/**
 * 
 */
package liwei;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;

/**
 * @author 李炜
 *	建立软件的开始框架
 */
public class MyFrame extends JFrame
{
	/**
	 * the constructor of the main frame(starting  frame).this frame includes input panel,menus and some 
	 * methods added so that they can get enough information from other element
	 * 框架的构造函数，此框架会自动添加输入面板，命令栏,并使得这些
	 * 元素之间能互相操纵
	 */
	public MyFrame()
	{
		setTitle("TextEditor");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout(0,0));
		Toolkit kit = Toolkit.getDefaultToolkit();
		Main.screenSize = kit.getScreenSize();
		frameSize = new Dimension((int)(Main.screenSize.width*0.8), (int)(Main.screenSize.height*0.8));
		setSize(frameSize.width, frameSize.height);
		setLocation((Main.screenSize.width-frameSize.width)/2,(Main.screenSize.height-frameSize.height) / 2);
		//add a window listener so that we can remind the client to save before exiting.
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				 int option = JOptionPane.showConfirmDialog(null,"退出前需要保存吗？","退出",JOptionPane.YES_NO_CANCEL_OPTION);
				 switch (option)
				{
				case JOptionPane.OK_OPTION:
					setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
					JFileChooser fileChooser = new JFileChooser();
					int returnVal = fileChooser.showOpenDialog(
							null);
						if(returnVal == JFileChooser.APPROVE_OPTION) {
							textPanel.writeFile(fileChooser.getSelectedFile());
						}
					break;
				case JOptionPane.CANCEL_OPTION:
				case JOptionPane.CLOSED_OPTION:
					setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
					break;
				case JOptionPane.NO_OPTION:
					setDefaultCloseOperation(EXIT_ON_CLOSE);
					break;
				
				default:
					break;
				}
			}
		});
		textPanel = new MyPanel();
		textPanel.setVisible(true);
		add(textPanel);
		myMenuBar = new MyMenuBar();
		setJMenuBar(myMenuBar);
		myMenuBar.setMyPanel(textPanel);
	}
	public MyPanel getMyPanel()
	{
		return textPanel;
	}
	private Dimension frameSize;
	private MyPanel textPanel;
	private MyMenuBar myMenuBar;
	
}
